package com.cdie.myschool.service.impl;

import com.cdie.myschool.common.R;
import com.cdie.myschool.domain.dto.StudentCostDTO;
import com.cdie.myschool.domain.entity.Calendar;
import com.cdie.myschool.domain.entity.ECardAccount;
import com.cdie.myschool.domain.entity.ECardTransaction;
import com.cdie.myschool.domain.vo.StudentCostVO;
import com.cdie.myschool.mapper.StudentCostMapper.StudentCostMapper;
import com.cdie.myschool.service.StudentCostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author 雷金臣199000347
 * @date 2022/10/12 - 19:06
 */
@Service
public class StudentCostServiceImpl implements StudentCostService {
    @Autowired
    private StudentCostMapper studentCostMapper;
    @Override
    public R<StudentCostVO> getStudentCost(StudentCostDTO studentCostDTO) {
        StudentCostVO sCVO=new StudentCostVO();
        double tCost=0;
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c=studentCostMapper.getCalendar(studentCostDTO);
        String b=f.format(c.getBeginTime());
        String begin=b.replace("-","").replace(" ","").replace(":","");
        long beginTime=Long.valueOf(begin);

        String e=f.format(c.getEndTime());
        String end=e.replace("-","").replace(" ","").replace(":","");
        long endTime=Long.valueOf(end);
        ECardAccount ec=studentCostMapper.getECardId(studentCostDTO);
        List<ECardTransaction> list= studentCostMapper.getECTList(c.getSemesterId(),ec.getECardId());

        for (int i = 0; i <list.size() ; i++) {
            String tsp=f.format(list.get(i).getTimestamp());
            String ts=tsp.replace("-","").replace(" ","").replace(":","");
            long timeStamp=Long.valueOf(ts);
            if(timeStamp>=beginTime&&timeStamp<=endTime){
                tCost=tCost+list.get(i).getTransactionAmount();
            }
        }
        System.out.println(tCost/5);
        sCVO.setTCost(String.valueOf(tCost));
        sCVO.setAVCost(String.valueOf(tCost/5));

        return R.Success("学生消费详情查询成功！",sCVO);
    }


}
